package org.tuestudent.grouppa;
import java.util.ArrayList;

import android.widget.LinearLayout.LayoutParams;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<ChatMessage> {
   private ArrayList<ChatMessage> messages;
   public CustomListViewAdapter(Context context, int textViewResourceId, ArrayList<ChatMessage> messages) {
        super(context, textViewResourceId, messages);
        this.messages = messages;
}
  @Override
  public View getView(int pos, View v, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.row, parent, false);
        //define the textview
        TextView tv = (TextView) v.findViewById(R.id.Message);
        //get the next message
        ChatMessage message = messages.get(pos);
        //if the message is not null
        if(message != null){
        LayoutParams lp = (LayoutParams) tv.getLayoutParams();
        //if the author is data, which is the tag for date items
        if(message.author.equals("date")){
            //get the date and set it as the text
            tv.setText(message.text);
            //set the background
            tv.setBackgroundResource(R.drawable.chat_item_normal);
            //center the message
            lp.gravity = Gravity.CENTER;
        } else {
            //if the message is not from the user himself/herself, display message, timestamp and user 
            if (!message.author.equals(Main.getContext().currentUserName)){
                //check if the minutes contain only 1 digit (lower than 10)
                if (message.date.getMinutes()<10){
                    tv.setText(message.author + ":\n" + message.text + " (" + message.date.getHours() + ":0" 
                            + message.date.getMinutes() + ")");
                } else {
                    tv.setText(message.author + ":\n" + message.text + " (" + message.date.getHours() + ":" 
                            + message.date.getMinutes() + ")");
                }
                //set the background
                tv.setBackgroundResource(R.drawable.chat_item_normal);
                //set open space from the left
                lp.leftMargin = 10;
                //align left
                lp.gravity = Gravity.LEFT;
            } else {
                //else the message is from the user, only display message and time
                if (message.date.getMinutes()<10){
                    tv.setText(message.text + " (" + message.date.getHours() + ":0" 
                            + message.date.getMinutes() + ")");
                } else {
                    tv.setText(message.text + " (" + message.date.getHours() + ":" 
                            + message.date.getMinutes() + ")");
                }
                //set the background
                tv.setBackgroundResource(R.drawable.chat_item_normal);
                //set margin right
                lp.rightMargin = 10;
                //align right
                lp.gravity = Gravity.RIGHT;
            }
            
        }
        //add the layout to the textview
        tv.setLayoutParams(lp);
        }
        //return the view
        return v;
  }
} 